//
//  DSZContentViewController.m
//  7.13苏宁框架
//
//  Created by app01 on 15/7/18.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZContentViewController.h"
#import "DSZcontentCell.h"
#import "ContentHeaderView.h"
#import "ContentModel.h"
#import "ContentHeaderView.h"
#import "Common.h"
#import "DSZShowViewController.h"
#import "UMSocial.h"
#import "DSZGZXQViewController.h"
#import "DSZTJGroupModel.h"
#import "DSZTJSuperModel.h"


@interface DSZContentViewController ()<ClickBtnDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property(weak,nonatomic)ContentHeaderView *headerView;
@property(weak,nonatomic)UIImageView *bgImgae;
@property(weak,nonatomic)UILabel *label;
@property(assign,nonatomic)CGFloat height;

@end

@implementation DSZContentViewController

static NSString * const reuseIdentifier = @"ContentCell";
static NSString * const headerIdentifier  = @"ContentHeader";


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Topic" bundle:nil];
    DSZContentViewController *vc=[story instantiateViewControllerWithIdentifier:@"content"];
    return vc;
    
}


-(void)viewWillAppear:(BOOL)animated{
    DSZShowViewController *show = (DSZShowViewController*)self.tabBarController;
    [show hiddenShowTabbar:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"话题详情";
    
    //NSArray *a = _cotentCollectionArray;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"DSZcontentCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ContentHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];

    
    UIButton *rigntItem = [UIButton buttonWithType:UIButtonTypeCustom];
    rigntItem.frame = CGRectMake(0, 0, 25, 20);
    [rigntItem setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [rigntItem setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateHighlighted];
    [rigntItem addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:rigntItem];
    self.navigationItem.rightBarButtonItem = right;
    
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.center.x-35, self.view.height-60, 70, 70)];
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"takepartin"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"takepartin"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(takePartIn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    }


-(void)takePartIn{
            UIImagePickerController *imagePk = [[UIImagePickerController alloc] init];
            // 2.判断图库是否可用打开
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
            {
                // 3.设置打开图库的类型
                imagePk.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
                imagePk.delegate = self;
    
                // 4.打开图片选择控制器
                [self presentViewController:imagePk animated:NO completion:nil];
            }
    
}

-(void)share{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"559f858467e58e8e9b00188e"
                                      shareText:@"逗了。。。"
                                     shareImage:[UIImage imageNamed:@"1"]
                                shareToSnsNames:@[UMShareToSina,UMShareToTencent,UMShareToQzone,UMShareToRenren,UMShareToDouban,UMShareToSms]
                                       delegate:nil];
}





#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _cotentCollectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DSZcontentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.contentModel = _cotentCollectionArray[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DSZGZXQViewController *vc = [[DSZGZXQViewController alloc]init];
    NSArray *array = [DSZTJSuperModel modelArrayWithFilename:@"Show.plist"];
    NSDictionary *dic= [array[1] keyValues];
    NSArray *groupArray=dic[@"group"];
    NSArray *groupModelArray=[DSZTJGroupModel modelArrayWithDictArray:groupArray];
    [vc XqGroupModel:groupModelArray[1]];
    [self.navigationController pushViewController:vc animated:YES];
    
}


//自定义段头
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (_label.tag == 0) {
         return CGSizeMake(320, 250);
    }else{
        return CGSizeMake(320, 250 + _height);
    }
    
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    ContentHeaderView *headerView = [[ContentHeaderView alloc]init];
    ContentHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
    headerView.topicModel = _headerModel;
    
    UIButton *btn = (UIButton *)[headerView viewWithTag:100];
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn1 = (UIButton *)[headerView viewWithTag:101];
    [btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *bgView = (UIImageView *)[headerView viewWithTag:102];
    _bgImgae = bgView;
    _headerView = headerView;
    
    _headerView.delegate = self;
    return headerView;
}

-(void)click:(UIButton *)btn{
    _headerView.selectBtn.selected = NO;
    btn.selected = !btn.selected;
    _headerView.selectBtn = btn;
    _bgImgae.left = btn.left;
}

-(void)clickChangeSize:(UILabel *)label height:(CGFloat)size{
    _label = label;
    
    _height = size;
    if (label.tag == 0) {
        label.tag = 1;
    }else{
        label.tag = 0;
    }
    [self.collectionView reloadData];
    [_headerView layoutSubviews];
}


@end
